package cn.zxh.csdn_maven.atomy.auth;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.json.JSONObject;


import cn.zxh.csdn_maven.base.exception.ReuSuceeCondiFailException;
import cn.zxh.csdn_maven.base.pojo.Cookies;
import cn.zxh.csdn_maven.work.flow.download.WorkFlowde;


@SuppressWarnings(value={"unused","deprecation"})
public class DownLoadChains {

	private Logger logger = Logger.getLogger(Login.class);
	/** 模拟请求的工具类 */
	private HttpClient client = null;
	public DownLoadChains() {
		client = new DefaultHttpClient();
	}
	public void start(String base_url,String target_url,boolean authority) throws Exception{
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("download_url", base_url);
		Map<String, Object> map = WorkFlowde.getInstance().startExec(client, params);
		if(map==null){
			throw new Exception("获取下载过程中出错");
		}
		JSONObject json = (JSONObject) map.get("data");
		if(json==null || json.get("actionUrl")==null){
			throw new Exception("获取真实地址失败");
		}
		String succ = json.get("succ").toString();
		String action_url = json.getString("actionUrl");
		if(null==action_url||"".equals(action_url)){
			throw new ReuSuceeCondiFailException("获取真实下载地址失败"); 
		}
		Pattern p = Pattern.compile("filename(.*?)%27%27(.*?)%22&");
        Matcher m = p.matcher(action_url);
        ArrayList<String> strs = new ArrayList<String>();
        while (m.find()) {
            strs.add(m.group(2));            
        } 
        String fileName = URLDecoder.decode(strs.get(0), "UTF-8");
        if(authority){
        	validSucc(succ);
        }
        DownLoad.startDownLoad(action_url, target_url+"/"+map.get("suffix_data".toString()), Cookies.getCookies(), fileName);
	}
	public boolean validSucc(String succ) throws ReuSuceeCondiFailException{
		/*succ=2003 or 2001分别表示下载且评论了 、无需积分下载 这两种方式可以放行
		 * 其他情况情况抛给外部处理。是否选择付费下载
		 */
		if("2011".equals(succ)){
			throw new ReuSuceeCondiFailException("未登录");
		}else if("2006".equals(succ)){
			//积分和C币都不足
			throw new ReuSuceeCondiFailException("积分和C币都不足");
		}else if("2007".equals(succ)){
			throw new ReuSuceeCondiFailException("积分不足,但有c币");
		}else if("2004".equals(succ)){
			throw new ReuSuceeCondiFailException("积分下载");
		}else if("2005".equals(succ)){
			//vip下载
			throw new ReuSuceeCondiFailException("vip下载");
		}else if("2022".equals(succ)){
			throw new ReuSuceeCondiFailException("24小时内下载次数超过20次");
		}else if("2002".equals(succ)){
			throw new ReuSuceeCondiFailException("非VIP,需积分下载且积分足够");
		}else if("2013".equals(succ)){
			throw new ReuSuceeCondiFailException("账号被锁");
		}else if("2003".equals(succ)){
			//已经下载过该资源且已评论过，不再扣除积分的弹框
			throw new ReuSuceeCondiFailException("已经下载过该资源且已评论过，不再扣除积分的弹框");
		}else if("1000".equals(succ)){
			//已下载过且没有评论过，弹出去评论框
			throw new ReuSuceeCondiFailException("已下载过且没有评论过，弹出去评论框");
		}
		return true;
	}
}
